﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Comm;
using HslCommunication;
using Server;
using HslCommunication.Core;

namespace SortInUi
{
    /// <summary>
    /// 状态命令枚举
    /// </summary>
    public enum CMD
    {
        强制放行 = 2,
        强制推出 = 0,
        强制验证 = 1
    }

    public partial class CfgFrm : Form
    {
        private static CfgFrm view = new CfgFrm();
        private CfgFrm()
        {
            InitializeComponent();
            this.Closed += CfgFrm_Closed;
        }

        private void CfgFrm_Closed(object sender, EventArgs e)
        {
            view = null;
        }

        private void CfgFrm_Load(object sender, EventArgs e)
        {
            Task.Run(() =>
             {
                 while (true)
                 {
                     OperateResult<int> line1Enable = PlcHelper.GetTagVal("Line1Enable");
                     OperateResult<int> line2Enable = PlcHelper.GetTagVal("Line2Enable");
                     OperateResult<int> InLineEnable = PlcHelper.GetTagVal("InLineEnable");
                     ResetBtnColor();
                     if (line1Enable.IsSuccess)
                     {
                         switch (line1Enable.Content)
                         {
                             case (int)CMD.强制推出:
                                 SetColorBtn(btnForeLine1, line1Enable.Content);
                                 break;
                             case (int)CMD.强制放行:
                                 SetColorBtn(btnPassLine1, line1Enable.Content);
                                 break;
                             case (int)CMD.强制验证:
                                 SetColorBtn(btnNormalLine1, line1Enable.Content);
                                 break;
                         }
                     }

                     if (line2Enable.IsSuccess)
                     {
                         switch (line2Enable.Content)
                         {
                             case (int)CMD.强制推出:
                                 SetColorBtn(btnForeLine2, line2Enable.Content);
                                 break;
                             case (int)CMD.强制放行:
                                 SetColorBtn(btnPassLine2, line2Enable.Content);
                                 break;
                             case (int)CMD.强制验证:
                                 SetColorBtn(btnNormalLine2, line2Enable.Content);
                                 break;
                         }
                     }

                     if (InLineEnable.IsSuccess)
                     {
                         switch (InLineEnable.Content)
                         {
                             case (int)CMD.强制放行:
                                 SetColorBtn(btnInLineDisable, InLineEnable.Content);
                                 break;
                             case (int)CMD.强制验证:
                                 SetColorBtn(btnInLineEnable, InLineEnable.Content);
                                 break;
                         }
                     }
                     Thread.Sleep(500);
                 }
             });
        }
        public static CfgFrm GetView()
        {
            return view ?? (view = new CfgFrm());
        }

        private void SetColorBtn(Button btn, int val)
        {
            btn.SetColor(Color.Green);
        }
        private void btnInLine_Click(object sender, EventArgs e)
        {
            SetVal("InLineEnable", (short)CMD.强制放行);
        }

        private void btnPassLine1_Click(object sender, EventArgs e)
        {
            SetVal("Line1Enable", (short)CMD.强制放行);
        }

        private void btnPassLine2_Click(object sender, EventArgs e)
        {
            SetVal("Line2Enable", (short)CMD.强制放行);
        }

        private void btnNormalLine1_Click(object sender, EventArgs e)
        {
            SetVal("Line1Enable", (short)CMD.强制验证);
        }

        private void btnNormalLine2_Click(object sender, EventArgs e)
        {
            SetVal("Line2Enable", (short)CMD.强制验证);
        }

        private void btnForeLine1_Click(object sender, EventArgs e)
        {
            SetVal("Line1Enable", (short)CMD.强制推出);
        }

        private void SetVal(string no, short val)
        {
            var rlt = PlcHelper.SetTag(no, val);
            if (!rlt.IsSuccess)
            {
                string msg = $"写变量【{no}】数值【{val}】失败，原因{rlt.Message}";
                Logger.WritePlcLog(msg);
            }
        }

        private void btnForeLine2_Click(object sender, EventArgs e)
        {
            SetVal("Line2Enable", (short)CMD.强制推出);
        }

        private void btnInLineEnable_Click(object sender, EventArgs e)
        {
            SetVal("InLineEnable", (short)CMD.强制验证);
        }

        private void ResetBtnColor()
        {
            var btns = this.Controls;
            foreach (Control bt in btns)
            {
                var btn = bt as Button;
                if (btn == null)
                {
                    continue;
                }
                btn.BackColor = Color.Gray;

            }
        }
    }
}
